<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <title>验证手机</title>

   {include file="public/css"}
   {include file="public/js"}
</head>
<body>

{include file="public/header"}

<div class="layui-container fly-marginTop fly-user-main">
<div class="user-left-menu" >
{include file="public/left_menu"}
</div>


  <div class="fly-panel fly-panel-user user-right-content" pad20>
    <div class="layui-tab layui-tab-brief" lay-filter="user">
      <ul class="layui-tab-title">
        <li class="layui-this">
          手机认证
        </li>
      </ul>
      <div class="layui-tab-content" id="LAY_ucm" style="padding: 20px 0;">
        <form class="layui-form" action="" >
          <div class="layui-form-item">
            <label class="layui-form-label">手机号</label>
            <div class="layui-input-inline">
                 <div class="layui-form-mid layui-word-aux">{if !empty($user.mobile)}{$user.mobile}{else}未设置{/if}</div>
            </div>
            <div class="layui-form-mid layui-word-aux">
            {if $user.is_mobile ==1}
              <em style="color:#999;">（已认证）</em>
             {else}
               <em style="color:#c00;">（未认证）</em>
            {/if}
            {if $user.mobile}
            <a class="layui-form-a" style="color:#4f99cf;" href="{:url('set/index')}">更换手机号</a>
            {else}
            <a class="layui-form-a" style="color:#4f99cf;" href="{:url('set/index')}">绑定手机号</a>
            {/if}
          
            </div>

          </div>   

            <div class="layui-form-item">
            <label class="layui-form-label">短信验证码</label>
            <div class="layui-input-inline">
              <input type="text" name="smscode" required lay-verify="required" placeholder="短信验证码" autocomplete="off" class="layui-input">
            </div>
            <div class="layui-form-mid layui-word-aux">   <a class="layui-form-a" style="color:#4f99cf;" id="activate" href="javascript:;" data-mobile="{$user.mobile}" data-status="1">发送短信</a></div>
          </div>   


          <div class="layui-form-item">
            <div class="layui-input-block">
              <button class="layui-btn layui-btn-normal" lay-submit lay-filter="form">确认</button>
            
            </div>
          </div>
        </form>
      </div>
    </div>
  </div>
  
</div>

<script>
layui.use('form', function(){
  var form = layui.form;
  form.on('submit(form)', function(data){
    // layer.msg(JSON.stringify(data.field));
    $.post('{:url("validate/mobile")}',data.field,function(json){
          if(json.code ==0){
             layer.msg(json.msg);
             location.reload();
          }else{
            layer.msg(json.msg);
          }
    });
    return false;
  });

});
</script>
   <script>
// 发送验证码
 var id = "#activate"; //发送验证码按钮id
$(id).on('click',function(){
  var mobile = $(this).attr('data-mobile');
  var status = $(this).attr('data-status');
  if(status == 2){
     return false;
  }



  $.post("{:url('validate/sendSms')}",{mobile:mobile},
                  function(json) {
                    if(json.code == 0){
                           time(id); 
                            layer.msg(json.msg, {icon: 1,
                            skin: 'layui-layer-molv',});
                        
                    }else if(json.code == -1){
                           
                             layer.msg(json.msg, {icon:0,
                             skin: 'layui-layer-molv',});
                       
                    }else{
                            time(id,0);
                             layer.msg(json.msg, {icon:0,
                             skin: 'layui-layer-molv',});
                       
                    }
                    
                  });
  return false;

});

/**
 * [time 定时函数]
 * @param  {[type]} o    [绑定的dom对象id]
 * @param  {Number} wait [定时时间，0关闭定时 ]
 * @return {[type]}      [无返回值]
 */
function time(id,wait=60) {
  if (wait == 0) {
      //倒计时完成解禁按钮
      $(id).attr("data-status",1);
      $(id).html("发送邮件");

    } else {
   // 倒计时中 禁用按钮
      $(id).attr("data-status",2);
      $(id).html("重新发送邮件(" + wait + ")");
      wait--;
   interval = setTimeout(function() {
          time(id,wait);
       },1000)
    }
 }
</script>
</body>
</html>